package com.anzia.map;

import java.util.*;

/**
 *@ClassName Collection_Demo
 *@Description
 *@Author Anzia.
 *@Date 2021/4/10 17:56
 *@Version 1.0
 **/
public class Collections_Demo {
    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<>();

        list.add(10);
        list.add(11);
        list.add(43);
        list.add(543);
        list.add(23);
        list.add(34);
        list.add(5);
        System.out.println(list);

        //sort排序
        Collections.sort(list);
        System.out.println(list);

        //二分查找
        int i = Collections.binarySearch(list, 11);
        System.out.println(i);

        //复制，要等长才能复制
        ArrayList<Integer> list1 = new ArrayList<>();
        for (int j = 0; j < list.size(); j++) {
            list1.add(0);
        }
        Collections.copy(list1,list);
        System.out.println(list1);

        //反转
        Collections.reverse(list);
        System.out.println(list);

        //打乱
        Collections.shuffle(list1);
        System.out.println(list1);

        //补充：list转成数组
        System.out.println("----------");
        Integer[] array = list.toArray(new Integer[0]);
        System.out.println(array.length);
        System.out.println(Arrays.toString(array));

        //数组转成集合
        String[] names = {"张三","李四","王五"};
        //是一个受限集合，不能增加和删除
        List<String> asList = Arrays.asList(names);
        System.out.println(asList);
        //把基本类型数组转成集合时，需要修改为包装类型
        Integer[] nums = {100,200,300,400,500};
        List<Integer> integerList = Arrays.asList(nums);
        System.out.println(integerList);
    }
}
